use anyhow::Result;
use tracing_appender::{
    rolling, 
    non_blocking::WorkerGuard
};
use tracing_subscriber::fmt::writer::MakeWriterExt;
use tracing_subscriber;
use clia_local_time;
use time::macros::format_description;

pub fn get_log_subscriber() -> Result<WorkerGuard>{
    let file_appender = rolling::daily("./logs", "sys.log");
    let (file_writer, guard) = tracing_appender::non_blocking(file_appender);

    let stdout_writer =  std::io::stdout;
    let combined_writer = stdout_writer.and(file_writer);

    let local_timer = clia_local_time::LocalTime::with_timezone(
        format_description!("[year]-[month]-[day] [hour]:[minute]:[second].[subsecond digits:3]"),(8, 0, 0));

    tracing_subscriber::fmt()
        .with_writer(combined_writer)
        .with_ansi(true)
        .with_timer(local_timer)
        .with_max_level(tracing::Level::TRACE)
        .init();

    Ok(guard)

}